package com.pangolin.pangolinim.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.net.NetFormat;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.SPUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends BaseActivity {
    private Button mBtnRegister;
    private EditText mEtNick;
    private EditText mEtPwd;
    private View mVLineNick;
    private View mVLinePwd;
    private ImageView mIvSeePwd;

    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mBtnRegister.setEnabled(canRegister());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    @Override
    protected void init(){
    }

    @Override
    protected void initView(){
        setToolbarTitle(UIUtil.getString(R.string.register));
        mBtnRegister = findViewById(R.id.btnRegister);
        mEtNick = findViewById(R.id.etNick);
        mEtPwd = findViewById(R.id.etPwd);
        mVLineNick = findViewById(R.id.vLineNick);
        mVLinePwd = findViewById(R.id.vLinePwd);
        mIvSeePwd = findViewById(R.id.ivSeePwd);

        mEtNick.addTextChangedListener(watcher);
        mEtPwd.addTextChangedListener(watcher);

        mEtNick.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                mVLineNick.setBackgroundColor(UIUtil.getColor(R.color.green0));
            } else {
                mVLineNick.setBackgroundColor(UIUtil.getColor(R.color.line));
            }
        });
        mEtPwd.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                mVLinePwd.setBackgroundColor(UIUtil.getColor(R.color.green0));
            } else {
                mVLinePwd.setBackgroundColor(UIUtil.getColor(R.color.line));
            }
        });

        mIvSeePwd.setOnClickListener(v -> {

            if (mEtPwd.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
                mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }

            mEtPwd.setSelection(mEtPwd.getText().toString().trim().length());
        });
    }

    @Override
    protected void initListener(){
        mBtnRegister.setOnClickListener(v -> {
            String username = mEtNick.getText().toString();
            String passwd = mEtPwd.getText().toString();
            if(TextUtils.isEmpty(username) || TextUtils.isEmpty(passwd)){
                UIUtil.showToast("请输入用户名密码");
                return;
            }
            Map<String, Object> brocastMap = new HashMap<String, Object>();
            brocastMap.put("type" , BroadcastMsg.DATA_UI_REG);
            brocastMap.put("username" , username);
            brocastMap.put("passwd" , passwd);
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
            BroadcastSender(bundleUtil.getBundle());
        });
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){
        if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_DATA){
            if(bundleUtil.getData() != null){
                Map<String, Object> resMap = bundleUtil.getData();
                if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_REG){       //登陆请求返回
                    if((int)resMap.get("result") == 0){
                        UIUtil.showToast("用户注册成功，请牢记您的UID："+String.valueOf((int) resMap.get("uid")));
                        finish();
                    }else{
                        UIUtil.showToast(NetFormat.ResMsg((int) resMap.get("result")));
                    }
                }
            }
        }
    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_register;
    }

    private boolean canRegister() {
        int nickNameLength = mEtNick.getText().toString().trim().length();
        int pwdLength = mEtPwd.getText().toString().trim().length();
        if (nickNameLength > 0 && pwdLength > 0) {
            return true;
        }
        return false;
    }
}